let system = uni.getSystemInfoSync(); // 获取系统信息
/**检查是否打开GPS功能（android）**/
export const checkOpenGPSServiceByAndroid = () => {
    if (system.platform === 'android') { // 判断平台
        openGps()
    } else if (system.platform == "ios") {
        //苹果打开对应的通知栏
        uni.showModal({
            title: "定位权限开启提醒",
            content: "未开启定位，请前往设置！",
            confirmText: "去设置",
            success: function (res) {
                if (res.confirm) {
                    let app = plus.ios.invoke("UIApplication", "sharedApplication");
                    let setting = plus.ios.invoke("NSURL", "URLWithString:", "app-settings:");
                    plus.ios.invoke(app, "openURL:", setting);
                    plus.ios.deleteObject(setting);
                    plus.ios.deleteObject(app);
                }
            }
        });
    }
}
/**
 * 定位权限及弹出权限弹框
 * **/
let num = 0;
export const openGps = () => {
    plus.android.requestPermissions(
        ['android.permission.ACCESS_FINE_LOCATION'],
        function (resultObj) {
            console.log('resultObj---', resultObj);
            let result = 0;
            for (let i = 0; i < resultObj.granted.length; i++) {
                let grantedPermission = resultObj.granted[i];
                console.log(6, '已获取的权限：' + grantedPermission);
                result = 1
            }
            for (let i = 0; i < resultObj.deniedPresent.length; i++) {
                let deniedPresentPermission = resultObj.deniedPresent[i];
                console.log(5, '拒绝本次申请的权限：' + deniedPresentPermission);
                num += 1
                result = 0
            }
            for (let i = 0; i < resultObj.deniedAlways.length; i++) {
                let deniedAlwaysPermission = resultObj.deniedAlways[i];
                console.log(4, '永久拒绝申请的权限：' + deniedAlwaysPermission);
                num += 1
                result = -1
            }
            // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
            if (result != 1) {
                uni.showModal({
                    title: "定位权限开启提醒",
                    content: "未开启定位，请前往设置！",
                    confirmText: "去设置",
                    success: function (res) {
                        if (res.confirm) {
                            setTimeout(() => {
                                let Intent = plus.android.importClass("android.content.Intent");
                                let Settings = plus.android.importClass("android.provider.Settings");
                                let Uri = plus.android.importClass("android.net.Uri");
                                let mainActivity = plus.android.runtimeMainActivity();
                                let intent = new Intent();
                                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                let uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
                                intent.setData(uri);
                                mainActivity.startActivity(intent);
                            }, 0)
                        }
                    }
                });
            } else {
                // 因为安卓手机  手机授权之后还需要打开定位服务功能所有当手机授权之后 调用下面的方法(不是所有手机都可以检测到)
                let context = plus.android.importClass("android.content.Context")
                let locationManager = plus.android.importClass("android.location.LocationManager")
                let main = plus.android.runtimeMainActivity()
                let mainSvr = main.getSystemService(context.LOCATION_SERVICE)
                if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
                    uni.showModal({
                        title: '提示',
                        content: '请打开定位服务功能',
                        success(res) {
                            if (res.confirm) {
                                if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
                                    let Intent = plus.android.importClass('android.content.Intent');
                                    let Settings = plus.android.importClass('android.provider.Settings')
                                    let intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                                    main.startActivity(intent) // 打开系统设置GPS服务页面
                                } else { }
                            }
                        }
                    });
                    return
                }
            }
        },
        function (error) {
            console.log('申请权限错误：' + error.code + " = " + error.message);
            resolve({
                code: error.code,
                message: error.message
            });
        }
    );
}

